#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>

#define GLOBALMEM_SIZE 0x1000
#define MEM_CLEAR 0x1
#define GLOBALMEM_MAJOR 230

static int globalmem_major = GLOBALMEM_MAJOR;
module_param(globalmem_major, int, S_IRUGO);

struct globalmem_dev
{
    struct cdev c_dev;
    unsigned char mem[GLOBALMEM_SIZE];
    struct mutex mtx;
};
static struct globalmem_dev* globalmem_devp;

static const char* classname = "globalmem_class";
static struct class* globalmem_class;
static const char* devicename = "globalmem";
static const char* modname = "globalmem_mod";

static int globalmem_open(struct inode* inode, struct file* filp)
{
    filp->private_data = globalmem_devp;
    return 0;
}

static int globalmem_release(struct inode* inode, struct file* filp)
{
    return 0;
}

static long globalmem_ioctl(struct file* filp, unsigned int cmd, unsigned long arg)
{
    struct globalmem_dev* dev = filp->private_data;

    switch (cmd)
    {
    case MEM_CLEAR:
        mutex_lock(&dev->mtx);
        memset(dev->mem, 0, GLOBALMEM_SIZE);
        mutex_unlock(&dev->mtx);
        printk(KERN_INFO "globalmem is set to zero\n");
        break;
    default:
        return -EINVAL;
        break;
    }

    return 0;
}

static ssize_t globalmem_read(struct file* filp, char __user* buf, size_t size, loff_t* ppos)
{
    unsigned long p = *ppos;
    unsigned int count = size;
    int ret = 0;
    struct globalmem_dev* dev = filp->private_data;

    if (p >= GLOBALMEM_SIZE)
    {
        return 0;
    }
    if (count > GLOBALMEM_SIZE - p)
    {
        count = GLOBALMEM_SIZE - p;
    }

    mutex_lock(&dev->mtx);
    if (copy_to_user(buf, dev->mem + p, count))
    {
        ret = -EFAULT;
    }
    else
    {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "read %u bytes from %lu\n", count, p);
    }
    mutex_unlock(&dev->mtx);

    return ret;
}

static ssize_t globalmem_write(struct file* filp, const char __user* buf, size_t size, loff_t* ppos)
{
    unsigned long p = *ppos;
    unsigned int count = size;
    int ret = 0;
    struct globalmem_dev* dev = filp->private_data;

    if (p >= GLOBALMEM_SIZE)
    {
        return 0;
    }
    if (count > GLOBALMEM_SIZE - p)
    {
        count = GLOBALMEM_SIZE - p;
    }

    mutex_lock(&dev->mtx);
    if (copy_from_user(dev->mem + p, buf, count))
    {
        ret = -EFAULT;
    }
    else
    {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "written %u bytes from %lu\n", count, p);
    }
    mutex_unlock(&dev->mtx);

    return ret;
}

static loff_t globalmem_llseek(struct file* filp, loff_t offset, int orig)
{
    loff_t ret = 0;
    switch (orig)
    {
    case SEEK_SET:      //beginning
        if (offset < 0)
        {
            ret = -EINVAL;
            break;
        }
        if (offset > GLOBALMEM_SIZE)
        {
            ret = -EINVAL;
            break;
        }
        filp->f_pos = offset;
        ret = filp->f_pos;
        break;
    case SEEK_CUR:
        if (filp->f_pos + offset > GLOBALMEM_SIZE)
        {
            ret = -EINVAL;
            break;
        }
        if (filp->f_pos + offset < 0)
        {
            ret = -EINVAL;
            break;
        }
        filp->f_pos += offset;
        ret = filp->f_pos;
        break;
    default:
        ret = -EINVAL;
        break;
    }

    return ret;
}

static const struct file_operations globalmem_fops = 
{
    .owner = THIS_MODULE,
    .llseek = globalmem_llseek,
    .read = globalmem_read,
    .write = globalmem_write,
    .unlocked_ioctl = globalmem_ioctl,
    .open = globalmem_open,
    .release = globalmem_release,
};

static void globalmem_setup_cdev(struct globalmem_dev* dev, int index)
{
    int err;
    int devno = MKDEV(globalmem_major, index);
    
    cdev_init(&dev->c_dev, &globalmem_fops);
    dev->c_dev.owner = THIS_MODULE;
    err = cdev_add(&dev->c_dev, devno, 1);

    if (err)
    {
        printk(KERN_NOTICE "Error %d adding globalmem%d\n", err, index);
    }
}

static int __init globalmem_init(void)
{
    int ret;
    dev_t devno = MKDEV(globalmem_major, 0);

    if (globalmem_major)
    {
        ret = register_chrdev_region(devno, 1, modname);
    }
    else
    {
        ret = alloc_chrdev_region(&devno, 0, 1, modname);
        globalmem_major = MAJOR(devno);
    }
    if (ret < 0)
    {
        return ret;
    }

    globalmem_devp = kzalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
    if (!globalmem_devp)
    {
        ret = -ENOMEM;
        goto fail_malloc;
    }

    mutex_init(&globalmem_devp->mtx);

    globalmem_setup_cdev(globalmem_devp, 0);

    globalmem_class = class_create(THIS_MODULE, classname);
    if (IS_ERR(globalmem_class))
	{
		printk("class_create error!\n");
		return PTR_ERR(globalmem_class);
	}
    device_create(globalmem_class, NULL, devno, NULL, devicename);

    return 0;

fail_malloc:
    unregister_chrdev_region(devno, 1);
    return ret;
}
module_init(globalmem_init);

static void __exit globalmem_exit(void)
{
    device_destroy(globalmem_class, MKDEV(globalmem_major, 0));
    class_destroy(globalmem_class);
    cdev_del(&globalmem_devp->c_dev);
    kfree(globalmem_devp);
    unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);
}
module_exit(globalmem_exit);

MODULE_LICENSE("GPL");
